﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;

public partial class Admin_Taikhoan : System.Web.UI.Page
{
    DADataContext da = new DADataContext();
    XL cl = new XL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            {
                Response.Redirect("/Online/Dangnhap.aspx");
            }
            else
            {
                var mcn = (from cn in da.Chucnangs where cn.Tenchucnang == "Quản lý tài khoản" select cn.Machucnang).FirstOrDefault();
                var kt = (from a in da.TaikhoanChucnangs where (a.Taikhoan == Session["user"].ToString() && a.Machucnang == mcn) select a).Count();
                if (kt == 1)
                {
                    var load = from cn in da.Accounts select cn;
                    GVTK.DataSource = load;
                    GVTK.DataBind();

                    if (string.IsNullOrEmpty(Request.QueryString["ac"]))
                    {
                        Edit.Visible = false;
                        btnThem1.Enabled = true;
                        btnSua1.Enabled = false;
                        txtMatKhau1.Enabled = true;
                        txtMatKhau2.Enabled = true;
                        txtTaiKhoan.Enabled = true;
                    }
                    else
                    {
                        string tk = Request.QueryString["ac"];
                        var get = (from x in da.Accounts where x.Taikhoan == tk select x).FirstOrDefault();
                        tdpq.Text = "Các quyền <i>" + get.Taikhoan + "</i> đang được sử dụng";
                        tdpq2.Text = "Các quyền <i>" + get.Taikhoan + "</i> đang không được sử dụng";
                        Edit.Visible = true;
                        var sd = from cn in da.Chucnangs where (from tkh in da.TaikhoanChucnangs where tkh.Taikhoan == tk && tkh.Machucnang != 0 select tkh.Machucnang).Contains(cn.Machucnang) select new { cn.Machucnang, cn.Tenchucnang };
                        gvsd.DataSource = sd;
                        gvsd.DataBind();

                        var ksd = from cn in da.Chucnangs where !(from tkh in da.TaikhoanChucnangs where (tkh.Taikhoan == tk) select tkh.Machucnang).Contains(cn.Machucnang) select new { cn.Machucnang, cn.Tenchucnang };
                        gvksd.DataSource = ksd;
                        gvksd.DataBind();
                        btnThem1.Enabled = false;
                        btnSua1.Enabled = true;

                        txtTaiKhoan.Text = get.Taikhoan;
                        txtHoTen.Text = get.Hoten;
                        rdNam.Checked = get.Gioitinh.Equals("Nam");
                        rdNu.Checked = get.Gioitinh.Equals("Nữ");
                        if (!get.Ngaysinh.ToString().Trim().Equals(""))
                            txtNgaySinh.Text = get.Ngaysinh.Value.ToString("dd/MM/yyyy");
                        txtEmail.Text = get.Email;
                        txtDiaChi.Text = get.Diachi;

                        txtMatKhau1.Enabled = false;
                        txtMatKhau2.Enabled = false;
                        txtTaiKhoan.Enabled = false;
                    }
                }
                else
                    Response.Redirect("/Online/Admin/Default.aspx");
            }
        }
    }
    protected void chonloaitk_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (chonloaitk.SelectedIndex == 0)
        {
            var load = from cn in da.Accounts select cn;
            GVTK.DataSource = load;
            GVTK.DataBind();
            Edit.Visible = false;
        }
        else
            if (chonloaitk.SelectedIndex == 1)
            {
                var load = from cn in da.Accounts where cn.Quyen == 1 select cn;
                GVTK.DataSource = load;
                GVTK.DataBind();
                Edit.Visible = false;
            }
            else
            {
                var load = from cn in da.Accounts where cn.Quyen == 0 select cn;
                GVTK.DataSource = load;
                GVTK.DataBind();
                Edit.Visible = false;
            }
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        StringCollection idConnllection = new StringCollection();
        string strids = string.Empty;

        for (int i = 0; i < gvsd.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)gvsd.Rows[i].Cells[0].FindControl("ckq1");
            if (ck != null)
            {
                if (ck.Checked == false)
                {
                    Label lb = (Label)gvsd.Rows[i].Cells[1].FindControl("macn");
                    strids = lb.Text;
                    idConnllection.Add(strids);
                }
            }
        }
        if (idConnllection.Count > 0)
        {
            string iDS = ""; int a;
            foreach (string id in idConnllection)
            {
                iDS += id + ",";
            }
            string[] mang = iDS.Split(',');
            for (int i = 0; i < mang.Length - 1; i++)
            {
                a = int.Parse(mang[i]);

                var xoa = da.TaikhoanChucnangs.SingleOrDefault(p => p.Machucnang == a && p.Taikhoan == Request.QueryString["ac"]);
                if (xoa != null)
                {
                    da.TaikhoanChucnangs.DeleteOnSubmit(xoa);
                    da.SubmitChanges();
                    var sd = from cn in da.Chucnangs where (from tkh in da.TaikhoanChucnangs where tkh.Taikhoan == Request.QueryString["ac"] && tkh.Machucnang != 0 select tkh.Machucnang).Contains(cn.Machucnang) select new { cn.Machucnang, cn.Tenchucnang };
                    gvsd.DataSource = sd;
                    gvsd.DataBind();
                    var ksd = from cn in da.Chucnangs where !(from tkh in da.TaikhoanChucnangs where (tkh.Taikhoan == Request.QueryString["ac"]) select tkh.Machucnang).Contains(cn.Machucnang) select new { cn.Machucnang, cn.Tenchucnang };
                    gvksd.DataSource = ksd;
                    gvksd.DataBind();
                    tb.Text = "";
                }
            }

        }
        else
            tb.Text = "<i>Không có bản ghi nào được chọn!</i>";
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        StringCollection idConnllection = new StringCollection();
        string strids = string.Empty;

        for (int i = 0; i < gvksd.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)gvksd.Rows[i].Cells[0].FindControl("ckq2");
            if (ck != null)
            {
                if (ck.Checked == true)
                {
                    Label lb = (Label)gvksd.Rows[i].Cells[1].FindControl("macn");
                    strids = lb.Text;
                    idConnllection.Add(strids);
                }
            }
        }
        if (idConnllection.Count > 0)
        {
            string iDS = ""; int a;
            foreach (string id in idConnllection)
            {
                iDS += id + ",";
            }
            string[] mang = iDS.Split(',');
            for (int i = 0; i < mang.Length - 1; i++)
            {
                a = int.Parse(mang[i]);
                TaikhoanChucnang tcn = new TaikhoanChucnang();
                tcn.Taikhoan = Request.QueryString["ac"];
                tcn.Machucnang = a;

                da.TaikhoanChucnangs.InsertOnSubmit(tcn);
                da.SubmitChanges();
                var sd = from cn in da.Chucnangs where (from tkh in da.TaikhoanChucnangs where tkh.Taikhoan == Request.QueryString["ac"] && tkh.Machucnang != 0 select tkh.Machucnang).Contains(cn.Machucnang) select new { cn.Machucnang, cn.Tenchucnang };
                gvsd.DataSource = sd;
                gvsd.DataBind();
                var ksd = from cn in da.Chucnangs where !(from tkh in da.TaikhoanChucnangs where (tkh.Taikhoan == Request.QueryString["ac"]) select tkh.Machucnang).Contains(cn.Machucnang) select new { cn.Machucnang, cn.Tenchucnang };
                gvksd.DataSource = ksd;
                gvksd.DataBind();
                tb.Text = "";
            }

        }
        else
            tb.Text = "<i>Không có bản ghi nào được chọn!</i>";
    }

    protected void ckChon2_CheckedChanged(object sender, EventArgs e)
    {
        if (ckChon2.Checked == true)
        {
            for (int i = 0; i < gvksd.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)gvksd.Rows[i].Cells[0].FindControl("ckq2");
                if (ck != null)
                {
                    ck.Checked = false;
                }
            }
        }
        else
        {
            for (int i = 0; i < gvksd.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)gvksd.Rows[i].Cells[0].FindControl("ckq2");
                if (ck != null)
                {
                    ck.Checked = true;
                }
            }
        }
    }
    protected void ckChon1_CheckedChanged(object sender, EventArgs e)
    {
        if (ckChon1.Checked == true)
        {
            for (int i = 0; i < gvsd.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)gvsd.Rows[i].Cells[0].FindControl("ckq1");
                ck.Checked = false;
            }
        }
        else
        {
            for (int i = 0; i < gvsd.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)gvsd.Rows[i].Cells[0].FindControl("ckq1");
                ck.Checked = true;
            }
        }
    }
    protected void btnThem1_Click(object sender, EventArgs e)
    {
        Account ac = new Account();
        ac.Taikhoan = txtTaiKhoan.Text.Trim();
        ac.Matkhau = cl.mahoa(txtMatKhau1.Text.Trim());
        ac.Hoten = txtHoTen.Text.Trim();
        ac.Gioitinh = rdNam.Checked ? "Nam" : "Nữ";
        ac.Diachi = txtDiaChi.Text.Trim();
        ac.Email = txtEmail.Text.Trim();
        if (!txtNgaySinh.Text.Trim().Equals(""))
            ac.Ngaysinh =DateTime.Parse(txtNgaySinh.Text.Trim().Split('/')[1] + "/" + txtNgaySinh.Text.Trim().Split('/')[0] + "/" + txtNgaySinh.Text.Trim().Split('/')[2]);
        ac.Quyen = 1;
        da.Accounts.InsertOnSubmit(ac);
        da.SubmitChanges();
        Response.Redirect("/Online/Admin/Taikhoan.aspx");
    }
    protected void btnSua1_Click(object sender, EventArgs e)
    {
        string m = Request.QueryString["ac"];
        var l = (from a in da.Accounts where a.Taikhoan == m select a).FirstOrDefault();
        l.Hoten = txtHoTen.Text.Trim();
        
        if (!txtNgaySinh.Text.Trim().Equals(""))
            l.Ngaysinh = DateTime.Parse(txtNgaySinh.Text.Trim().Split('/')[1] + "/" + txtNgaySinh.Text.Trim().Split('/')[0] + "/" + txtNgaySinh.Text.Trim().Split('/')[2]);
        l.Diachi = txtDiaChi.Text.Trim();
        l.Email = txtEmail.Text.Trim();
        l.Gioitinh = rdNam.Checked ? "Nam" : "Nữ";

        da.SubmitChanges();
        Response.Redirect("/Online/Admin/Taikhoan.aspx");
    }
    protected void btnXoa1_Click(object sender, EventArgs e)
    {
        StringCollection idConnllection = new StringCollection();
        string strids = string.Empty;

        for (int i = 0; i < GVTK.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GVTK.Rows[i].Cells[0].FindControl("ckq1");
            if (ck != null)
            {
                if (ck.Checked == true)
                {
                    HyperLink lb = (HyperLink)GVTK.Rows[i].Cells[1].FindControl("tk");
                    strids = lb.Text;
                    idConnllection.Add(strids);
                }
            }
        }
        if (idConnllection.Count > 0)
        {
            string iDS = ""; string a="";
            foreach (string id in idConnllection)
            {
                iDS += id + ",";
            }
            string[] mang = iDS.Split(',');
            for (int i = 0; i < mang.Length - 1; i++)
            {
                a = mang[i];
                var xoa = da.Accounts.SingleOrDefault(p => p.Taikhoan == a);
                if (xoa != null)
                {
                    da.Accounts.DeleteOnSubmit(xoa);
                    da.SubmitChanges();
                }
            }
            Response.Redirect("/Online/Admin/Taikhoan.aspx");
        }
    }
    protected void btnHuy1_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Online/Admin/Taikhoan.aspx");
    }
}
